<?php
/**
 * Created by PhpStorm.
 * User: zhangligang
 * Date: 17/5/8
 * Time: 下午1:25
 */

namespace App\Http\Presenter;


use App\Http\Models\Order;

class OrderPresenter implements Presenter
{

    /**
     *
     */
    public function __construct()
    {
        //
    }

    /**
     * 格式化订单列表
     * @param mixed $orderList
     * @return array
     */
    public function formatOrderList($orderList)
    {

        $results = [];

        if (count($orderList)) {

            foreach ($orderList as $order) {

                if ($order['pid'] === 0) {

                    $result = $this->formatOrder($order);

                }

                foreach ($orderList as $value) {

                    if ($result['id'] === $value->pid) {

                        $result['sub_order'] = $this->formatOrder($value);
                    }
                }

                $results[] = $result;

            }

        }

        return $results;

    }

    /**
     * 格式化订单
     * @param Order $order
     * @return array
     */
    public function formatOrder(Order $order)
    {

        if (!$order) return [];

        return $order->pid === 0 ? [

            'id' => $order->id,

            'order_sn' => $order->order_sn,

            'pid' => $order->pid


        ]
         : [

            'id' => $order->id,

            'order_sn' => $order->order_sn,

            'pid' => $order->pid,

            'goods_id' => $order->goods_id,

            'goods_num' => $order->goods_num,

            'goods_name' => $order->goods()->title(),

            'store_id' => $order->store_id,

            'store_name' => $order->store_name,

            'buyer_id' => $order->buyer_id,

            'buyer_name' => $order->buyer_name,

            'buyer_email' => $order->buyer_email,

            'discount' => $order->discount,

            'goods_amount' => $order->goods_amount,

            'amount' => $order->amount,

            'deliver_code' => $order->deliver_code,

            'deliver_fee' => $order->deliver_fee,

            'status' => $order->status,

            'message' => $order->message,

        ];

    }

}